package com.leyou.item.controller;


import com.leyou.common.pojo.PageResult;
import com.leyou.item.dto.CartDTO;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import com.leyou.item.vo.SpuDTO;
import com.leyou.item.vo.SpuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("")
public class GoodsController {

    @Autowired
    private GoodsService goodsService ;


    /**
     *
     *
     * @param page
     * @param rows
     * @param saleable
     * @param key
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuVO>> querySpuByPaeg(
            @RequestParam(value = "page",defaultValue = "1" )Integer page,
            @RequestParam(value = "rows",defaultValue = "5" )Integer rows,
            @RequestParam(value = "saleable",required = false )Boolean saleable,
            @RequestParam(value = "key",required = false ) String key

    ) {
        PageResult<SpuVO> spuVOPageResultList = goodsService.querySpuByPaeg(page, rows, saleable, key);
        return ResponseEntity.ok(spuVOPageResultList);
    }


    /**
     * 商品修改
     *
     * @param spuDTO
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 商品新增
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     *  根据spu 的id 查询详情detail
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> queryDetailByid(@PathVariable("id") Long spuId) {
        SpuDetail  spuDetail = goodsService.queryDetailByid(spuId);
        return ResponseEntity.ok(spuDetail);
    }

    /**
     *  根据spu 查询下面的所有 sku
     *
     * @param id
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long id) throws Exception {
        List<Sku> skuList = this.goodsService.querySkuBySpuId(id);
        return ResponseEntity.ok(skuList);
    }

    /**
     * 根据spu的id 查询spu
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) throws Exception {
        SpuDTO spu = this.goodsService.querySpuById(id);
        return ResponseEntity.ok(spu);
    }

    /**
     * 根据spu 的id集合查询下面的所有 sku
     *
     * @param ids
     * @return
     */
    @GetMapping("/sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuBySpuIds(@RequestParam("ids") List<Long> ids) throws Exception {
        List<Sku> skuList = this.goodsService.querySkuBySpuIds(ids);
        return ResponseEntity.ok(skuList);
    }


    /**
     * 减少库存
     *
     * @param carts
     * @return
     */
    @PostMapping("/stock/decrease")
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDTO> carts) {
        goodsService.decreaseStock(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
